/*
 ** writen by hanv
 ** n2.vietha@gmail.com
 */
package vn.vng.research.fsd.da;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

import vn.vng.research.fsd.Configuration;

public class SchoolRefinerDA {
	private static String SCHOOL_REFINE_PATH = Configuration.loadSchoolRefinePath();
	private static final String SPLITTER = "\\|";
	
	private static Map<String, Integer> RAW_TO_ID = new HashMap<String, Integer>();
	private static Map<Integer, String> ID_TO_NAME = new HashMap<Integer, String>();
	
	private static Map<String, Integer> NAME_TO_ID = new HashMap<String, Integer>();
	
	public static final int UNKNOWN_ID = 10000;

	public static void fetch() {
		
		try {
			FileInputStream fis = new FileInputStream(SCHOOL_REFINE_PATH);
			DataInputStream dis = new DataInputStream(fis);
			BufferedReader br = new BufferedReader(new InputStreamReader(dis));
			String line;
			int counter = 1;
			int lineCounter = 1;
			
			ID_TO_NAME.put(UNKNOWN_ID, "Unknown");
			NAME_TO_ID.put("Unknown", UNKNOWN_ID);
			
			System.out.println("Started loading mapping from: " + SCHOOL_REFINE_PATH);
			long start = System.currentTimeMillis();
			
			while ((line = br.readLine()) != null) {
				String[] elements = line.split(SPLITTER);
				if (elements.length != 2) {
					// create new Exception Class later
					throw new Exception("Invalid file format", new Throwable(
							"Invalid key-value pair at line " + lineCounter + ": "
									+ line));
				} else {					
					Integer existID = NAME_TO_ID.get(elements[1]);
					if (existID != null){
						RAW_TO_ID.put(elements[0], existID);
					} else {
						Integer id = UNKNOWN_ID + counter;
						RAW_TO_ID.put(elements[0], id);
						ID_TO_NAME.put(id, elements[1]);
						NAME_TO_ID.put(elements[1], id);
						counter++;
					}
					
					
					
				}
				lineCounter++;
			}
			
			dis.close();
			
			long end = System.currentTimeMillis();

			System.out.println("Loaded " + lineCounter + " line(s) with "
					+ counter + " school(s) name after " + (end - start)
					+ "ms.");
			
		} catch (Exception e) {
			// handle outside later
			e.printStackTrace();
		}

	}
	
	public static int getID(String raw) throws Exception{
		//neu chua khoi tao thi sao?
		if (RAW_TO_ID.isEmpty()){
			throw new Exception("RAW_TO_ID map has not been initialize");
		}
		Integer id = RAW_TO_ID.get(raw);
		if (id == null){
			return UNKNOWN_ID;
		} else {
			return id;
		}
	}
	
	public static String getSchoolName(String raw) throws Exception{
		//neu chua khoi tao thi sao?
		if (RAW_TO_ID.isEmpty()){
			throw new Exception("RAW_TO_ID map has not been initialize");
		}
		if (ID_TO_NAME.isEmpty()){
			throw new Exception("ID_TO_NAME map has not been initialize");
		}
		return ID_TO_NAME.get(getID(raw));
	}
	
	public static String getSchoolName(int id) throws Exception{
		//neu chua khoi tao thi sao?
		if (ID_TO_NAME.isEmpty()){
			throw new Exception("ID_TO_NAME map has not been initialize");
		}
		return ID_TO_NAME.get(id);
	}

	public static void setSCHOOL_REFINE_PATH(String sCHOOL_REFINE_PATH) {
		SCHOOL_REFINE_PATH = sCHOOL_REFINE_PATH;
	}
	
	
}
